/**
 * Created by Lwang on 2017-03-30.
 */

import Util from "utils"
import $ from "jquery";


var myFinanceCtrl = ['$scope', '$stateParams', '$state', '$location', '$http', '$timeout', '$rootScope', 'Http', function ($scope, $stateParams, $state, $location, $http, $timeout, $rootScope, Http) {
    $scope.isActive = 1;                        //活动active
    $scope.investList = "";                     //列表数组
    $scope.isHasData = true;                    //是否有数据
    var totalArr = [];                          //所有数据
    var totalPage = 0;                          //总页数
    var bool = true;                            //bool值
    var type = 0;                               //请求类型
    var page = 0;                               //页数

    //所有全部
    $scope.Jall = function () {
        totalArr = [];
        totalPage = 0;                          //总页数
        bool = true;                            //bool值
        type = 0;                               //请求类型
        page = 0;                               //页数
        $scope.isHasData = true;                    //是否有数据
        $scope.isActive = 1;
        investListAjax({type: 0});
    };
    //已认购
    $scope.Jhas = function () {
        totalArr = [];
        totalPage = 0;                          //总页数
        bool = true;                            //bool值
        type = 0;                               //请求类型
        page = 0;                               //页数
        $scope.isHasData = true;                    //是否有数据
        $scope.isActive = 2;
        investListAjax({type: 1});
    };
    //已回购
    $scope.Jbuy = function () {
        totalArr = [];
        totalPage = 0;                          //总页数
        bool = true;                            //bool值
        type = 0;                               //请求类型
        page = 0;                               //页数
        $scope.isHasData = true;                    //是否有数据
        $scope.isActive = 3;
        investListAjax({type: 2});
    };

    Http.request({
        url: Util.ajax.haveInvests,
        method: "get",
        success: (res) => {
            console.log(res);
            Http.handler(Util.merge(res,{
                success: function (result) {
                    if (result.result) {
                        investListAjax({})
                    }else{
                        location.replace("http://m.jshrdtz.com/tips2");
                    }
                }
            }));

        }
    });

    //加载更多
    $(window).scroll(function () {
        if ($state.current.name == "myFinance") {
            var dh = $(document).height(),
                wh = $(window).height(),
                scrollTop = $(window).scrollTop();
            if (dh == wh + scrollTop) {
                if (page < totalPage) {
                    if (bool) {
                        bool = false;
                        page++;
                        investListAjax({type: type, page: page});
                    }
                } else {
                    // Util.tips({
                    //     content: "已经到达最后一页!!!"
                    // })
                }
            }
        }
    });


    //我的投资列表
    function investListAjax({type = 0, page = 0, size = 10}) {
        Http.request({
            url: Util.ajax.custInvestList + "?type=" + type + "&page=" + page + "&size=" + size,
            method: "get",
            success: function (res) {
                Http.handler(Util.merge(res, {
                    success: (result) => {
                        totalPage = result.totalPages;
                        if (Util.getType(result.content) != "array") {
                            result.content = [];
                        }
                        result.content.map((_data, index) => {
                            totalArr.push(_data);
                        });
                        $scope.investList = totalArr;
                        if (totalArr.length == 0) {
                            $scope.isHasData = false;
                        }
                        bool = true;

                    },
                    error: (err) => {

                    }
                }))
            }
        })
    }

    //到期时间处理
    $scope.dateCancle = function (time) {
        var date = new Date(time);
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return year + "-" + month + "-" + day;
    }
    //投资周期处理
    $scope.monthCancle = function (day) {
        return Math.round(day / 30);
    }
//    是否可以回购
    $scope.ifBuyBack = function (time) {
        return (new Date().getTime() > time);
    }

}];
export {myFinanceCtrl};